package flexo.examples;

import flexo.math.*;
import flexo.scene.Node;
import flexo.graphic.Color;
import flexo.graphic.entity.particle.ParticleFactory;

public class Test_Graphic_Particles extends Test_Graphic_Base
{
	private static int xParticles = 10;
	private static int yParticles = 10;
	
	Node root;
	Node[][] partNode;
	Node axis;
	Node partNode3;
	Node gridNode;
	
	flexo.graphic.entity.ParticleSystem particles;
	flexo.graphic.entity.ParticleSystem particles2;
	flexo.graphic.entity.Entity grid;
	
	flexo.graphic.Camera cam;
	
	public static void main(String[] args)
	{
		run("flexo.examples.Test_Graphic_Particles");
	}
	
	public void init()
	{
		super.init();
		partNode = new Node[xParticles][yParticles];
		
		root = mainScene.addChild("root");
		graphic.getCamera(0).setBackgroundColor(new Color(0,0,0));
		
		gridNode = root.addChild("grid");
		
		flexo.graphic.Material mat = graphic.addMaterial("gridMat", "textureRepeat");
		flexo.graphic.ShaderParam param = mat.getShaderParam("tex");
		param.setValue("textures/grid.png");
		param = mat.getShaderParam("repeatU");
		param.setValue(50.0f);
		param = mat.getShaderParam("repeatV");
		param.setValue(50.0f);
		
		grid = graphic.addQuad(50, 50);
		grid.setMaterial("gridMat");
		grid.attachTo(gridNode);
		gridNode.setRotation(new flexo.math.Quaternion(new flexo.math.Vector(1,0,0),-90.0f));
		gridNode.setPosition(new flexo.math.Vector(0,-3,0));			
		
		axis = root.addChild("axis");
		partNode3 = axis.addChild("particles3");
		
		particles = graphic.addParticleSystem(10, 2.0f);		
		
		particles.addAffector(ParticleFactory.getSphereEmitter(0.3f));
		particles.addAffector(ParticleFactory.getLinePath(new Vector(0,2,0)));
		particles.addAffector(ParticleFactory.getTransparencyModifier());
		particles.setColor(new Color(1,0,0));
		
		particles2 = graphic.addParticleSystem(30, 1.0f);
		
		//particles2.addAffector(ParticleFactory.getSphereEmitter(0.5f));
		particles2.addAffector(ParticleFactory.getPointEmitter());
		particles2.addAffector(ParticleFactory.getLinePath(new Vector(0,1,0)));
		particles2.addAffector(ParticleFactory.getTransparencyModifier());
		particles2.setColor(new Color(0,0,1));
		particles2.setWorldNode(partNode3);
		
		mat = graphic.addMaterial("default", "default");
		flexo.graphic.ShaderParam pTex = mat.getShaderParam("tex");
		pTex.setValue("textures/particle.jpg");
		particles.setMaterial("default");
		particles.getMaterial().setBlending(flexo.graphic.Material.Blending.TRANSPARENT_ADD);
		
		for (int i = 0; i < xParticles; ++i)
		{
			for (int j = 0; j < yParticles; ++j)
			{
				partNode[i][j] = mainScene.addChild("particles["+i+","+j+"]");
				particles.attachTo(partNode[i][j]);
				partNode[i][j].setPosition(new Vector(i * 1,0,-j * 1));
			}
		}
		
		particles2.setMaterial("default");
		particles2.attachTo(partNode3);
		partNode3.setPosition(new Vector(1.5f,0,0));
	}

	public void cleanUp()
	{
	}

	public void update()
	{
		axis.rotate(Quaternion.createRotationY( 90 * getElapsedTime() ));		
	}
}
